package com.example.ywj.toutiao.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.News;

import java.util.List;

public class SearchItemAdapter extends BaseAdapter {
    private List<News> list ;
    private String baseUrl = Constant.BASEURL;
    public SearchItemAdapter(List<News>list){
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.search_item_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.tv1 = convertView.findViewById(R.id.search_item_title);
            viewHolder.iv1  = convertView.findViewById(R.id.search_item_pic);
            viewHolder.tv2 = convertView.findViewById(R.id.search_item_content);
            viewHolder.iv2 = convertView.findViewById(R.id.search_item_avatar);
            viewHolder.tv3 = convertView.findViewById(R.id.search_item_author);
            viewHolder.tv4 = convertView.findViewById(R.id.search_item_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tv1.setText(list.get(position).getTitle());
        viewHolder.tv2.setText(list.get(position).getContent());
        viewHolder.tv3.setText(list.get(position).getAuthor());
        viewHolder.tv4.setText(list.get(position).getTime());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
        RequestOptions requestOptions1 = new RequestOptions();
        requestOptions1.placeholder(R.drawable.boy).error(R.drawable.error_img);
        String images = list.get(position).getImages();
        String []image = images.split("&");
        String imgUrl1 = baseUrl+"public/images/"+image[0]+".jpg";
        Glide.with(parent.getContext()).load(imgUrl1).apply(requestOptions).into(viewHolder.iv1);
        String avatarUrl1 = baseUrl+"public/images/"+list.get(position).getAvatar()+".jpg";
        Glide.with(parent.getContext()).load(avatarUrl1).apply(requestOptions1).into(viewHolder.iv2);
        return convertView;
    }
    class ViewHolder{
        TextView tv1;
        ImageView iv1;
        TextView tv2;
        ImageView iv2;
        TextView tv3;
        TextView tv4;
    }
}
